<?
include('../controller/handler.php');
include('../controller/twitwords.php');
include('../scaffold/user.php');
include('../scaffold/tag.php');

$hd = new Handler();
$tw = new Twitwords();
$base_url = $hd->getBaseURL();
$user_searched = $_GET['user'];
$tag_searched = $_GET['tag'];
$page = (isset($_GET['page']) && $_GET['page']>1)? $_GET['page'] : 1;
$i = 0;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<?
	/****************
	 * HEAD MODULE
	 ***************/
	include('../module/head.inc.php');
	?>


	<title><?echo $hd->getTitle();?> - Search <?echo sprintf('%s%s', $user_searched, $tag_searched)?></title>
</head>

<body>
<?
/****************
 * HEADER MODULE
 ***************/
include('../module/header.inc.php');
?>
<div id="container">
	<?
	/****************
	 * SEARCH MODULE
	 ***************/
	include('../module/search.inc.php');
	?>
	<?
	if($user_searched!=NULL || $tag_searched!=NULL) {
		$hd->openDB();
		$i = $offset;
		if($user_searched!=NULL) {
			$url = "user:$user_searched";
			$quantity = $hd->getUsersPerPage();
			$offset = ($page-1)*$quantity;
			$users = $tw->searchUser($user_searched, $offset, $quantity);
			if(count($users)==0) {
				echo sprintf("<h1>No user matching <em>%s</em>...</h1>", $user_searched);
			}
			foreach($users as $user) {
				$i++;
				$style = ($i%2==0)? "even" : "odd";
				?>
				<div class="tag <?echo $style;?>">
				<img class="avatar" src="<?echo $user->getImage();?>" alt="Profile image" />
				<a class="username" href="<?echo $base_url;?>user/<?echo $user->getUsername();?>">
					<?echo eregi_replace(
						$user_searched, 
						sprintf('<span class="found">\\0</span>', $user_searched), 
						$user->getUsername()
					);?>
				</a>
				</div>
				<?
			}
		} elseif($tag_searched!=NULL) {
			$url = "tag:$tag_searched";
			$quantity = $hd->getTagsPerPage();
			$offset = ($page-1)*$quantity;
			$tags = $tw->searchTag($tag_searched, $offset, $quantity);
			if(count($tags)==0) {
				echo sprintf("<h1>No tag matching <em>%s</em>...</h1>", $tag_searched);
			}
			foreach($tags as $tag) {
				$i++;
				$style = ($i%2==0)? "even" : "odd";
				?>
				<div class="tag <?echo $style;?>">
				<img class="avatar" src="<?echo $tag->getUser()->getImage();?>" alt="Profile image" />
				<a class="username" href="<?echo $base_url;?>user/<?echo $tag->getUser()->getUsername();?>">
					<?echo $tag->getUser()->getUsername();?>
				</a> <span class="light">{ is a }</span>
				<p class="text">
					<?
					echo eregi_replace(
						$tag_searched, 
						sprintf('<span class="found">\\0</span>', $tag_searched), 
						$tag->getTag()
					);?>
				</p>
				<p class="date"><?echo $hd->deltaToTime(time(), strtotime($tag->getDate()));?></p>
				</div>
				<?
			}
		}
		$hd->closeDB();
	}
	?>
	
	<div id="credits"><?echo $hd->getCredits();?></div>
</div>
<div id="footer">
	<div id="nav">
		<?if($page>1) {?><a href="<?echo $base_url."search/".$url."/page:".($page-1);?>">&larr; previous</a> <?}?>
		<?if($i && $i>=$offset+$quantity) {?><a href="<?echo $base_url."search/".$url."/page:".($page+1);?>">next &rarr;</a><?}?>
	</div>
	<?
	/****************
	 * FOOTER MODULE
	 ***************/
	include('../module/footer.inc.php');
	?>
</div>

<?include('../module/analytics.inc.php');?>
</body>
</html>
